package com.zbh.uexam.configuration.spring.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import java.io.IOException;

/**
 * @Author zhoubh
 * @Date 2021/3/23 16:05
 * @Version 1.0
 * @description:
 */

@Component
public class CustomExpiredSessionStrategy implements SessionInformationExpiredStrategy {
    private static final Logger logger = LoggerFactory.getLogger(CustomExpiredSessionStrategy.class);

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException, ServletException {
        // Cookie[] cookies = sessionInformationExpiredEvent.getRequest().getCookies();
        // if (null != cookies) {
        //     for (int i = 0; i < cookies.length; i++) {
        //         String cookieName = cookies[i].getName();
        //         if (StringUtils.isNotBlank(cookieName) && (cookieName.equals("xzs"))) {
        //             Cookie cookie = new Cookie(cookies[i].getName(), "");
        //             cookie.setMaxAge(0);
        //             cookie.setPath(sessionInformationExpiredEvent.getRequest().getContextPath());
        //             cookie.setDomain(sessionInformationExpiredEvent.getRequest().getServerName());
        //             sessionInformationExpiredEvent.getResponse().addCookie(cookie);
        //         }
        //     }
        // }
        sessionInformationExpiredEvent.getRequest().getRequestDispatcher("/session/repeatUser").forward(sessionInformationExpiredEvent.getRequest(), sessionInformationExpiredEvent.getResponse());
        }
}
